package com.zrrd.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.OrderDetail;
import com.zrrd.reggie.bean.Orders;

import com.zrrd.reggie.bean.OrdersDto;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.service.OrderDetailService;
import com.zrrd.reggie.service.OrdersService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {


    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    //分页查询订单列表
    @GetMapping("/page")
    public R<Page> page(Integer page,
                        Integer pageSize,
                        String number,
                        String beginTime,
                        String endTime){

        //分页对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (number != null && !number.equals("")){
            wrapper.eq("number",number);
        }

        //日期范围查询
        if (beginTime != null && !beginTime.equals("")&& endTime!=null && !endTime.equals("")){
            wrapper.between("order_time",beginTime,endTime);
        }

        //分页查询
        ordersService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    //修改订单状态
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    //用户下单支付
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    //移动端的分页查询订单列表
    @GetMapping("/userPage")
    public R<Page> userPage(
            Integer page,Integer pageSize
    ){
        //查询订单
        Page<Orders> page1 = new Page<>(page,pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ThreadLocalUtil.getCurrentId());
        wrapper.orderByDesc("checkout_time");
        ordersService.page(page1,wrapper);

        //查询订单详细明细集合列表
        Page<OrdersDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(page1,dtoPage,"records");
        List<OrdersDto> dtoList = new ArrayList<>();
        for (Orders orders : page1.getRecords()) {
        //创建dto对象
            OrdersDto dto = new OrdersDto();
            BeanUtils.copyProperties(orders,dto);
            QueryWrapper<OrderDetail> qw = new QueryWrapper<>();
            qw.eq("order_id",orders.getId());
            //根据订单id 查询订单明细表
            List<OrderDetail> details = orderDetailService.list(qw);
            dto.setOrderDetails(details);
            dtoList.add(dto);
        }

        //设置dto的records
        dtoPage.setRecords(dtoList);

        return R.success(dtoPage);
    }

    //再来一单
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        //调用业务层放方法
        ordersService.again(orders);
        return R.success(null);
    }
}
